package com.itheima.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.HomeRecommendProduct;
import com.itheima.mapper.HomeRecommendProductMapper;
import com.itheima.service.IHomeRecommendProductService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 人气推荐商品表 服务实现类
 * </p>
 *
 * @author fukeke
 * @since 2022-11-11
 */
@DubboService
public class HomeRecommendProductServiceImpl extends ServiceImpl<HomeRecommendProductMapper, HomeRecommendProduct> implements IHomeRecommendProductService {

    @Autowired
    HomeRecommendProductMapper homeRecommendProductMapper;

    @Override
    public PageBeanVo findByPage(Integer pageNum, Integer pageSize, String productName, Integer recommendStatus) {

        LambdaQueryWrapper<HomeRecommendProduct>lqw=new LambdaQueryWrapper<>();
        Page<HomeRecommendProduct>page=new Page<>(pageNum,pageSize);

        lqw.like(productName!=null,HomeRecommendProduct::getProductName,productName);
        lqw.eq(recommendStatus!=null,HomeRecommendProduct::getRecommendStatus,recommendStatus);
        lqw.orderByDesc(HomeRecommendProduct::getSort);
        homeRecommendProductMapper.selectPage(page,lqw);
        return new PageBeanVo(pageNum,pageSize,page.getTotal(),page.getRecords());

    }

    @Override
    public void addNew(HomeRecommendProduct homeRecommendProduct) {
        homeRecommendProductMapper.insert(homeRecommendProduct);
    }

    @Override
    public void delete(Integer[] ids) {
        if(ArrayUtil.isNotEmpty(ids))
        {
            for (Integer id : ids) {
                homeRecommendProductMapper.deleteById(id);

            }
        }
    }

    @Override
    public void update(Integer[] ids, Integer recommendStatus) {

        if(ArrayUtil.isNotEmpty(ids))
        {
            for (Integer id : ids) {
                HomeRecommendProduct homeRecommendProduct = homeRecommendProductMapper.selectById(id);
                homeRecommendProduct.setRecommendStatus(recommendStatus);
                homeRecommendProductMapper.updateById(homeRecommendProduct);

            }
        }
    }

    @Override
    public void sort(Integer id, Integer sort) {
        HomeRecommendProduct homeRecommendProduct = homeRecommendProductMapper.selectById(id);

        homeRecommendProduct.setSort(sort);
        homeRecommendProductMapper.updateById(homeRecommendProduct);
    }
}
